package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WmThreadLocal;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

   @Autowired
   private FileStorageService fileStorageService;


    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile file) {
        try {
            //1.1 获取图片名称
            String originalFilename = file.getOriginalFilename();//获取原始名称，1.jpg
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));//获取后缀名
            String fileName= UUID.randomUUID().toString()+suffix;
            //1.上传图片到minio中
            String path = fileStorageService.uploadImgFile("", fileName, file.getInputStream());

            //2.新增wm_material表
            WmMaterial wmMaterial=new WmMaterial();
            wmMaterial.setUrl(path);
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);

            WmUser wmUser = WmThreadLocal.getWmUser();
            wmMaterial.setUserId(wmUser.getId());
          save(wmMaterial);
            return ResponseResult.okResult("上传图片成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"出现了异常情况");
        }
    }

    /**
     * 图片列表查询
     *   mybatisplus分页查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //1.参数校验
        if(dto.getPage()==null){
            dto.setPage(1);
        }
        if(dto.getSize()==null){
            dto.setSize(10);
        }
        //2.执行分页查询
        IPage<WmMaterial> ipage=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> queryWrapper= Wrappers.lambdaQuery();
        //是否收藏
        if(dto.getIsCollection()==1){//收藏
            queryWrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }

        //根据用户id查询
        queryWrapper.eq(WmMaterial::getUserId,WmThreadLocal.getWmUser().getId());

       ipage = page(ipage, queryWrapper);

        /**
         *  3.封装数据
         *  参数1表示当前页码
         *  参数2表示每页显示的条数
         *  参数3表示总条数
         */

        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) ipage.getTotal());
        result.setData(ipage.getRecords());


        return result;
    }
}
